<html>
<head>
<title>Pattern marker example with Three.js</title>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<style>
html,body {
	margin: 0;
	padding: 0;
	width: 100%;
	text-align: center;
	overflow-x: hidden;
}
.portrait canvas {
	transform-origin: 0 0;
	transform: rotate(-90deg) translateX(-100%);
}
.desktop canvas {
 	transform: scale(-1, 1);
}
</style>
</head>
<body>

<h1>NFT marker example with Three.js</h1>
<p>On Chrome on Android, tap the screen to start playing video stream.</p>
<p>Show  <a href="https://github.com/artoolkit/artoolkit5/blob/master/doc/Marker%20images/pinball.jpg">Pinball image</a> to camera to display a colorful object on top of it. Tap the screen to rotate the object.

<p>&larr; <a href="index.html">Back to examples</a></p>

<script async src="../vendor/jsartoolkit5/build/artoolkit.min.js"></script>
<script async src="../../vendor/three.js/build/three.js"></script>
<script async src="../vendor/jsartoolkit5/js/artoolkit.three.js"></script>

<script src="../vendor/jsartoolkit5/js/artoolkit.proxy.js"></script>

<script>

window.ARThreeOnLoad = function() {

	ARController.getUserMediaThreeScene({maxARVideoSize: 320, cameraParam: '../../../../../../data/data/camera_para.dat', 
	onSuccess: function(arScene, arController, arCamera) {

		var proxy = new ARProxy(arController, '../../../../../../data/data/camera_para.dat', function() { console.log("got new", arguments); });

		proxy.addEventListener('load', function(ev) {
			console.log('ARController loaded', ev);

			this.processingDone = false;
			this.loadNFTMarker('../../../../../../data/dataNFT/pinball', function(ev) {
				console.log('Created NFT marker', ev.result[0]);
			
				var markerRoot = this.arController.createThreeNFTMarker(ev.result[0]);
				markerRoot.add(sphere);
				arScene.scene.add(markerRoot);
				this.processingDone = true;
			});
		});

		document.body.className = arController.orientation;

		var renderer = new THREE.WebGLRenderer({antialias: true});
		if (arController.orientation === 'portrait') {
			var w = (window.innerWidth / arController.videoHeight) * arController.videoWidth;
			var h = window.innerWidth;
			renderer.setSize(w, h);
			renderer.domElement.style.paddingBottom = (w-h) + 'px';
		} else {
			if (/Android|mobile|iPad|iPhone/i.test(navigator.userAgent)) {
				renderer.setSize(window.innerWidth, (window.innerWidth / arController.videoWidth) * arController.videoHeight);
			} else {
				renderer.setSize(arController.videoWidth, arController.videoHeight);
				document.body.className += ' desktop';
			}
		}

		document.body.insertBefore(renderer.domElement, document.body.firstChild);

		var rotationV = 0;
		var rotationTarget = 0;

		renderer.domElement.addEventListener('click', function(ev) {
			ev.preventDefault();
			rotationTarget += 1;
		}, false);

		var sphere = new THREE.Mesh(
			new THREE.SphereGeometry(0.5, 8, 8),
			new THREE.MeshNormalMaterial()
		);
		sphere.material.shading = THREE.FlatShading;
		sphere.position.z = 40;
		sphere.position.x = 80;
		sphere.position.y = 80;
		sphere.scale.set(80,80,80);

		var torus = new THREE.Mesh(
			new THREE.TorusGeometry(0.3, 0.2, 8, 8),
			new THREE.MeshNormalMaterial()
		);
		torus.material.shading = THREE.FlatShading;
		torus.position.z = 0.5;
		torus.rotation.x = Math.PI/2;

		arController.process = function(image) {};

		var tick = function() {
			proxy.process(arScene.video);

			rotationV += (rotationTarget - sphere.rotation.z) * 0.05;
			sphere.rotation.z += rotationV;
			torus.rotation.y += rotationV;
			rotationV *= 0.8;

			arScene.renderOn(renderer);
			requestAnimationFrame(tick);
		};

		tick();

	}});

	delete window.ARThreeOnLoad;

};

if (window.ARController && ARController.getUserMediaThreeScene) {
	ARThreeOnLoad();
}
</script>

</body>
</html>
